package com.gc.web.controller.business;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.gc.business.domain.Attachment;
import com.gc.business.service.IAttachmentService;
import com.gc.common.base.Result;
import com.gc.common.support.Convert;
import com.gc.common.utils.DateUtils;
import com.gc.common.utils.StringUtils;
import com.gc.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gc.common.annotation.Log;
import com.gc.common.enums.BusinessType;
import com.gc.business.domain.ContractInfo;
import com.gc.business.service.IContractInfoService;
import com.gc.framework.web.base.BaseController;
import com.gc.common.utils.poi.ExcelUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 合同 控制层
 * 
 * @author liujinteng
 * @date 2020-11-09
 */
@Slf4j
@Controller
@RequestMapping("/business/contractInfo")
public class ContractInfoController extends BaseController {
    private String prefix = "business/contractInfo";
	
	private final IContractInfoService contractInfoService;

	private final IAttachmentService attachmentService;

	@Autowired
	public ContractInfoController(IContractInfoService contractInfoService,
								  IAttachmentService attachmentService) {
		this.contractInfoService = contractInfoService;
		this.attachmentService = attachmentService;
	}

	/**
	*  列表页
	*/
	@RequiresPermissions("business:contractInfo:view")
	@GetMapping
	public String contractInfo(){
	    return prefix + "/contractInfo";
	}

	/**
	 * 新增页
	 */
	@GetMapping("/add")
	public String add(){
		return prefix + "/add";
	}

	/**
	 * 编辑页
	 */
	@GetMapping("/edit/{contractId}")
	public String edit(@PathVariable("contractId") Long contractId, ModelMap mmap){
		ContractInfo contractInfo = contractInfoService.getById(contractId);
		mmap.put("contractInfo", contractInfo);
		List<Attachment> attachmentList = new ArrayList<>();
		if (StringUtils.isNotEmpty(contractInfo.getContractAttachment())){
			if(contractInfo.getContractAttachment().contains(",")){
				String[] split = contractInfo.getContractAttachment().split(",");
				for (String attachmentId : split) {
					Attachment attachment = attachmentService.getById(Long.valueOf(attachmentId));
					if(StringUtils.isNotNull(attachment)){
						attachmentList.add(attachment);
					}
				}
			}else {
				if(!NumberUtil.isNumber(contractInfo.getContractAttachment())){
					return "error/404";
				}
				Attachment attachment = attachmentService.getById(Long.valueOf(contractInfo.getContractAttachment()));
				if(StringUtils.isNotNull(attachment)){
					attachmentList.add(attachment);
				}
			}
			mmap.put("attachmentList",attachmentList);
		}
		return prefix + "/edit";
	}
	
	/**
	 * 查询合同列表
	 */
	@RequiresPermissions("business:contractInfo:list")
	@PostMapping("/list")
	@ResponseBody
	public Result list(ContractInfo contractInfo){
		startPage();
        List<ContractInfo> list = contractInfoService.list(contractInfo);
		return Result.success(getDataTable(list));
	}

	/**
	 * 新增保存合同
	 */
	@RequiresPermissions("business:contractInfo:add")
	@Log(title = "合同", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(ContractInfo contractInfo){
		String userName = ShiroUtils.getSysUser().getUserName();
		contractInfo.setCreateBy(userName);
		contractInfo.setCreateTime(DateUtil.date());
		contractInfoService.save(contractInfo);
		return Result.success();
	}

	/**
	 * 修改保存合同
	 */
	@RequiresPermissions("business:contractInfo:edit")
	@Log(title = "合同", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(ContractInfo contractInfo){
		String userName = ShiroUtils.getSysUser().getUserName();
		contractInfo.setUpdateBy(userName);
		contractInfo.setUpdateTime(DateUtil.date());
		contractInfoService.updateById(contractInfo);
		return Result.success();
	}
	
	/**
	 * 删除合同
	 */
	@RequiresPermissions("business:contractInfo:remove")
	@Log(title = "合同", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public Result remove(String ids){
		Long[] longIds = Convert.toLongArray(ids);
		contractInfoService.removeByIds(Arrays.asList(longIds));
		return Result.success();
	}

	/**
	 * 导出合同列表
	 */
	@RequiresPermissions("business:contractInfo:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(ContractInfo contractInfo){
		List<ContractInfo> list = contractInfoService.list(contractInfo);
		ExcelUtil<ContractInfo> util = new ExcelUtil<>(ContractInfo.class);
		util.exportExcel(list, "contractInfo");
		return Result.success();
	}
}
